//
//  RAPAccountModel.swift
//  COPYCAT-Weibo
//
//  Created by 闫萌 on 12/29/15.
//  Copyright © 2015 RendezvousAuParadis. All rights reserved.
//

import UIKit

//{
//    "access_token": "ACCESS_TOKEN",
//    "expires_in": 1234,
//    "remind_in":"798114",
//    "uid":"12341234"
//}

class RAPAccountModel: NSObject, NSCoding {
    
    /** access_token */
    var access_token: String?
    /** 过期时间，单位秒 */
    var expires_in: Int = 0 {
        didSet { // 存方法
            // 计算过期的日期
            expireDate = NSDate(timeIntervalSinceNow: NSTimeInterval(expires_in))
        }
    }
    /** 过期日期 */
    var expireDate: NSDate?
    /** user id */
    var uid: String?
    
    /** screen_name	string	用户昵称 */
    var screen_name: String?
    /** avatar_large	string	用户头像地址（大图），180×180像素 */
    var avatar_large: String?
    
    
    override var description: String { // 重写这个计算属性
        
        return "access_token: \(access_token), expires_in: \(expires_in), uid: \(uid), screen_name: \(screen_name), avatar_large: \(avatar_large)"
        }
    
    init(dict: [String: NSObject]) {
        super.init()
        
        setValuesForKeysWithDictionary(dict)
    }
    
    // MARK: - <NSCoding>
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(screen_name, forKey: "screen_name")
        aCoder.encodeObject(avatar_large, forKey: "avatar_large")
        aCoder.encodeInteger(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expireDate, forKey: "expireDate")
        // 由于父类是NSObject，其并未遵守NSCoding协议，所以不必调用父类的encodeWithCoder
    }
    
    required init?(coder aDecoder: NSCoder) {
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid")  as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_in = aDecoder.decodeIntegerForKey("expires_in")
        expireDate = aDecoder.decodeObjectForKey("expireDate") as? NSDate
        
        RAPLog()
    }
}

extension RAPAccountModel {
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        // 故意留空
    }
}
